package com.atguigu.gulimall.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.atguigu.gulimall.order.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    //在支付宝创建的应用的id
    private   String app_id = "9021000145698967";

    // 商户私钥，您的PKCS8格式RSA2私钥
    public static String merchant_private_key = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCZKe7UO2uKv2A8WvKl0aE0QHrlhdLBtzl7ueVEoGY8A29yeNOBVDEbI+NQMiD4MlNwjuGExShKByxUV/8F3wu/O1IzXLepmssPCtKfc8ku4l8X5i0zIhzG0UGaWviAaO/QjLwHaQVjz+RMmgJW9vsSIWKghiAhE6UeIMSuO4dt6XQ3y2mK8CVDZQHPRc/bWoth///P+Fr01FaF+AK1O3SuSF9dnv2jhSfrH9/hGZcX7r/Zl/LjCSxXbTNECQvGDQxK84dY8Vebr2R8F3cL1vnc6rmN4yr0aNa6QoJuhRal1boPOfHIHh1B5iKb2YfgiDM7RwWeIZ/xlh+j3CkZ65F3AgMBAAECggEAbeN0ArkPE2Dy/tqRtP6OFhsdEYfOCkYW6/oX0AD02KHuV8bwmJ17DenVsTgGB4yz6NHaY5PAsDwtReZMhj3dA7rf4xHmg70XaxdsvhIoiYbIizENxN34n3yLAN+74wVm6c3PKdHjmBUTyxHEE/jncb6aMGmKq7CYiuEfu5HiPKxhXaqzYaPqwO2unnUgfh8F5v7wJs3IpmbYdWDk76iWp/oyHFhZK02ogXKRYNRP8qWFeLrniRHf+hqIyNgljmD9yBI72Q2lSucR0Nom7UxxZTpK6qvf9LYh0XlkQ2OHUaT4PxaxDlH18FbRqnUuLj/u0ErBQVAcKls5jIXAQLPv4QKBgQDU8t645qq9mbB2+6OERw/8uR0/RrsAIYNeyfkN2yJgdxVFhGAmkLeblqTdwP2xORtL0IH66IBXQ0gQ+NoFa6x+W49kKQGljzV53BnuI0BHToY3llx7hikX6dngfL/pGng/jj4ThVD1ogSRBj2l6b2kC2hPny+qig7VisxcPX3ApQKBgQC4IOdIioiLVKNcqBnn0YiUrmQYmMG++zdc73MsQIiBFKIirTgD8B30FclqVhZcsljuBf3Fmzn395SZgnkOjLEyhvc57WWAZFkuKKtH1scYYYKJ6R2dMwX59xc3k5p6dTGQWMmal17ep+Qrzxvpb92AF/iTIQUuNa6ZZJHaohWy6wKBgFdHeAvW1xJA5LfPqZR0v9O4V4lwxwJluf9mzsJkF3HsPPayK+qClA6cLtSI3jH+Evb+7ddV5gxeEjKKnGluWVOs2VeXCwq9aJCaqrIVFqpjP9WWti8WrM8VVOySlOHIrJJ4DW9Yy/bxQefi4++VntmWp1lDCHlgfnX9nocL2PfVAoGAXIPj0dYRj3hicK7h/VGRv6r0DkvKpYwSIDcL9oMiU0EryiKgz5G5ayf7QNuqdr8B4e7OuJgrA0W5u3wMGVxluE6KL+fxJRbGnGPA0+K9vKlZ7rNbtt2pNhSJ7NnUN41k7vM9sNg3ycYv5jc0/hDACZmBw2NG+BXu+ZHHm0PvhSsCgYEAtQSiVWD5hV22gm6l47sQqCXWcUNW064kI3ZjPAdD4k4racvWlsqZQHuM0DC98eEvJa6XeFuhSa6xucAvEvku4z08tBaJfewBYeRpIs7hhe5vP30SO3kQJMCi40FPVk9UoKY4x3xSANVIW4URYGcH4bXpzA3tmHZ+WcjWnLuP8OU=";

    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    public static String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAkRsHWEm+7rDYCUYLwjGq0fgbmZ3IyemB7lOtu5SQPC+bAkSwUE84dRJrHYyg/Fx0WSVi0r4Xbx4m1z1mVJ9JnaMfnmL7tpkAHrErkjRTLNo/lWL0GERTlEYKliag7whzTQbhPtGvKXnpWZFqp6ldEAmmfIeuSMM0oMehySNMD2ymYbCUg0xmHgmspS4xFA74D1hseNw7Y+5ipM/MubN5kNThuzcNEag+GxcBYyxuZ6G0loIq47BrUW5CcX8fsv5v3h9GiZa8ioVFcEMdqxRvCtvbj4gNXEoT72nwXZampZ4/3cfKun2tkD5Q0uGkj982NV7EHxdvGn+PSiKFUWkROwIDAQAB";

    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    private  String notify_url;

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    private  String return_url = "http://member.gulimall.com/memberOrder.html";

    // 签名方式
    private  String sign_type = "RSA2";

    // 字符编码格式
    private  String charset = "utf-8";

    // 支付宝网关
    public static String gatewayUrl = "https://openapi-sandbox.dl.alipaydev.com/gateway.do";

    // 支付宝网关
    public static String log_path = "D:\\malll\\tmp";

    public  String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"timeout_express\":\"1m\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应："+result);

        return result;

    }
}
